import pandas as pd
import dash_bootstrap_components as dbc
from dash import html

df = pd.read_excel('./sample/导出模板.xlsx')

test_cols = ['失效编号', '产品PH2', 'UAES物料号', '失效点位', '结论编号', '分析结论', '失效点编号', '问题编号', '问题标题']
df_test = df.loc[df['产品PH1'] == 'CON', test_cols]
df_test['结论编号'].fillna('22M0000000', inplace=True)
df_test['分析结论'].fillna('分析中', inplace=True)

df_Mode = df_test.groupby(['结论编号', '分析结论'])['失效编号'].count().reset_index()
df_Mode.columns = ['no', 'name', 'qty']


def buildModeCard(row):
    return html.Div(
        id="_".join(['mode', row['no']]),
        children=[
            html.Span(row['name']),
            dbc.Badge(id="_".join(['badge', row['no']]), children=row['qty'])
        ],
        className="mode"
    )


def buildCategoryCard(row):
    return dbc.Card(
        children=[
            dbc.CardHeader(
                dbc.Input(value=row['name'], type="text", className="cate input"),
                className="cate card-header"),
            dbc.CardBody(
                html.Div(
                    id="_".join(['cate', row['no']]),
                    children=buildModeCard(row),
                    className="cate"
                ),
                className="cate card-body"
            ),
        ],
        className="cate card")


ModeList = []
CategoryList = []
CategoryCardList = []
BadgeList = []
for i, row in df_Mode.iterrows():
    ModeList.append("_".join(['mode', row['no']]))
    CategoryList.append("_".join(['cate', row['no']]))
    BadgeList.append("_".join(['badge', row['no']]))
    CategoryCardList.append(buildCategoryCard(row))
# print(ModeList)
# print(CategoryList)


# Mode_list = df_Mode.to_dict(orient='records')

# df_problem = df_test[['问题编号', '问题标题']].dropna().drop_duplicates()
# df_problem.columns = ['no', 'name']
# problem_list = df_problem.to_dict(orient='records')
#
# df_Mode_problem = df_test[['结论编号', '问题编号']].dropna().drop_duplicates()
# df_Mode_problem.columns = ['source', 'target']
# df_Mode_problem['no'] = df_Mode_problem.apply(lambda row: "-".join([row['source'], row['target']]), axis=1)
# Mode_problem_list = df_Mode_problem.to_dict(orient='records')
#
# def buildNode(nodeRecord, label):
#     data = {'group': 'nodes',
#             'data': {"id": nodeRecord['no'],
#                      "label": label,
#                      "name": nodeRecord['name']}}
#     return data
#
#
# def buildEdge(edgeRecord, label):
#     data = {'group': 'edges',
#             'data': {"id": edgeRecord['no'],
#                      "label": label,
#                      "source": edgeRecord['source'],
#                      "target": edgeRecord['target'],
#                      }}
#     return data
#
#
# problem_eles = [buildNode(i, "problem") for i in problem_list]
# Mode_eles = [buildNode(i, "Mode") for i in Mode_list]
# elements = problem_eles + Mode_eles
#
# Mode_problem_edges = [buildEdge(i, "fixed_by") for i in Mode_problem_list]
# edges = Mode_problem_edges
